﻿using Foxit.PDF;
using Foxit.PDF.IO;
using Foxit.PDF.Imaging;
using Foxit.PDF.PageElements;
using Sustainalytics.Reporting.Framework;

namespace Sustainalytics.ReportGenerator.BL
{
    public class RatingControl : PageElement
    {
        public float Left { get; set; }
        public float Top { get; set; }
        public int Value { get; set; }

        private static byte[] _imageOn;
        private static byte[] _imageOff;

        private Group Elements()
        {
            var elements = new Group();

            var left = Left;

            for (var index = 0; index < 5; index++)
            {
                elements.Add(index <= 5-Value
                    ? new Image(ImageData.GetImage(_imageOn), left, Top, 0.25f)
                    : new Image(ImageData.GetImage(_imageOff), left, Top, 0.25f));

                left += 15;
            }

            return elements;
        }

        public override void Draw(PageWriter writer)
        {
            if (_imageOn == null)
                _imageOn = ImageManager.GetJpgImage(@"Images\leaf_on.jpg");
            if (_imageOff == null)
                _imageOff = ImageManager.GetJpgImage(@"Images\leaf_off.jpg");

            var elements = Elements();

            writer.Write_q_(true);
            elements.Draw(writer);
            writer.Write_Q(true);
        }
    }
}
